package com.joe.gulishop.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joe.gulishop.product.entity.BrandEntity;
import com.joe.gulishop.product.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.joe.gulishop.product.entity.CategoryBrandRelationEntity;
import com.joe.gulishop.product.service.CategoryBrandRelationService;
import com.joe.common.utils.PageUtils;
import com.joe.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author joe
 * @email joe@gmail.com
 * @date 2021-12-03 10:45:56
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
//获取当前品牌的列表
    @RequestMapping("/catelog/list")
    public R catelogList(@RequestParam("brandId") Long brandId){

        List<CategoryBrandRelationEntity> list=categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId)
        );

        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
            categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /*
    * http://localhost:88/api/product/categorybrandrelation/brands/list?t=1643027227201&catId=225
    * 根据品类获取所有的品牌，这个参数必须存在
    * */
    @GetMapping("/brands/list")
    public R brandsList(@RequestParam(value = "catId",required = true) Long catId){
       List<BrandEntity> brandEntityList= categoryBrandRelationService.queryBrandByCatelogId(catId);
       List<BrandVo> brandVoList =brandEntityList.stream().map((brandEntity)->{
            BrandVo brandVo=new BrandVo();
            brandVo.setBrandName(brandEntity.getName());
            brandVo.setBrandId(brandEntity.getBrandId());
            return brandVo;
        }).collect(Collectors.toList());
        return  R.ok().put("data",brandVoList);
    }
}
